package com.f1ashback.bus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.f1ashback.bus.Qo.FeedbackQo;
import com.f1ashback.bus.Vo.FeedBackVo;
import com.f1ashback.bus.common.AjaxResult;
import com.f1ashback.bus.entity.FeedBack;
import com.f1ashback.bus.entity.User;
import com.f1ashback.bus.mapper.FeedBackMapper;
import com.f1ashback.bus.mapper.UserMapper;
import com.f1ashback.bus.service.FeedBackService;
import com.f1ashback.bus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FeedBackServiceImpl extends ServiceImpl<FeedBackMapper, FeedBack> implements FeedBackService {

    @Autowired
    private FeedBackMapper feedBackMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean appadd(FeedBack feedBack) {
        feedBack.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        log.warn(feedBack.toString());
        if (feedBack.getFeedback() == null || feedBack.getFeedback() == "") return false;
        feedBack.setFeedId(setFeedBackId(feedBack.getUserId()));
        int insert = feedBackMapper.insert(feedBack);
        if (insert <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public List<FeedBackVo> listAll() {
        List<FeedBackVo> res = new ArrayList<>();
        List<FeedBack> list = feedBackMapper.selectList(null);
        for (FeedBack feedBack : list) {
            FeedBackVo vo = new FeedBackVo();
            BeanUtils.copyProperties(feedBack, vo);
            User oneUser = userService.getOneUser(feedBack.getUserId());
            vo.setUsername(oneUser.getUsername());
            res.add(vo);
        }
        return res;
    }

    @Override
    public Integer deleteF(Long feedId) {
        int i = feedBackMapper.deleteById(feedId);
        return i;
    }

    @Override
    public Map<String, List<String>> getFeedBackAndUser(Long userId) {
        Map<String, List<String>> map = new HashMap<>();
        User oneUser = userService.getOneUser(userId);
        List<String> list = feedBackMapper.listByUserId(oneUser.getUserId());
        map.put(oneUser.getUsername(), list);
        return map;
    }

    @Override
    public AjaxResult queryFeedback(FeedbackQo feedbackQo) {
        Map<String, List<FeedBackVo>> res = new HashMap<>();
        List<FeedBackVo> resList = new ArrayList<>();
        Long userId = userMapper.selectUserIdByName(feedbackQo.getUsername());
        FeedBack queryfb = new FeedBack();
        queryfb.setUserId(userId);
        List<FeedBack> list = feedBackMapper.queryFeedBack(queryfb);
        for (FeedBack feedBack : list) {
            FeedBackVo feedBackVo = new FeedBackVo();
            BeanUtils.copyProperties(feedBack, feedBackVo);
            User oneUser = userService.getOneUser(feedBack.getUserId());
            feedBackVo.setUsername(oneUser.getUsername());
            resList.add(feedBackVo);
        }
        res.put("res",resList);
        return AjaxResult.success(res);
    }

    private Long setFeedBackId(Long s) {
        return Long.valueOf(String.valueOf(s).substring(3)) +
                Long.valueOf(String.valueOf(System.currentTimeMillis()).substring(7));
    }
}
